# -*- coding: utf-8 -*-
# @Time        :2024/9/29 下午3:56
# @Author      :文刀水寿
# @File        : 05_全国疫情可视化地图开发.py
"""
 @Description :
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *

# 读取数据文件
f_yq = open("D:\PythonCharm\python资源\疫情.txt", "r", encoding='UTF-8')
yq_data = f_yq.read()

# 关闭文件
f_yq.close()

# 取到各省数据
# 将字符串 json转换为python的字典
yq_dict = json.loads(yq_data)
yq_list = yq_dict["areaTree"][0]["children"]
# 组装每个省份和确诊人数为元组， 并各个省的数据都封装入列表内
data_list = []
for province_data in yq_list:
    province_name = province_data["name"]
    province_confirm = province_data["total"]["confirm"]
    data_list.append((province_name, province_confirm))

print(data_list)
# 创建地图对象
map = Map()

# 添加数据
map.add("各省份确诊人数", data_list, "china")

# 设置全局配置，定制分段的视觉映射
map.set_global_opts(
    title_opts=TitleOpts(title='全国疫情地图'),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
            {"min": 100, "max": 99, "label": "10-99人", "color": "#FFFF99"},
            {"min": 1000, "max": 499, "label": "99-499人", "color": "#FF9966"},
            {"min": 5000, "max": 999, "label": "499-9人", "color": "#FF6666"},
            {"min": 10000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
            {"min": 100000, "label": "10000以上", "color": "#990033"},
        ]
    )
)

# 绘图
map.render("全国疫情地图.html")
